// pages/xingzuanBuy/xingzuanBuy.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    password: '', // 支付密码
    number: '', // 卖出数量
    sum: '', // 卖出总价
    unitPrice: '' // 卖出单价
  },
  // 获取input中用户输入卖出数量的值
  getNum(e) {
    this.setData({
      number: e.detail.value
    })
    var sum = 0,
      number = this.data.number,
      unitPrice = this.data.unitPrice
    sum = number * unitPrice
    this.setData({
      sum: sum.toFixed(2)
    })
    // console(this.data.number)
  },
  // 点击卖出星钻按钮提交相应数据
  Sell() {
    var that = this
    // 判断用户是否有支付密码
    wx.request({
      url: app.globalData.baseUrl + '/user/checkPaymentCode',
      data: {
        xcxOpenId: app.globalData.xcxOpenId
      },
      success(res) {
        var sum = 1
        var number = that.data.number
        var unitPrice = that.data.unitPrice
        sum = number * unitPrice
        that.setData({
          sum
        })
        // console.log(res)
        if (res.data.data == false) {
          that.setData({
            showPayPwdInput: false
          })
          wx.showToast({
            title: '请先设置支付密码',
            icon: 'none'
          })
          wx.navigateTo({
            url: '/pages/setNewPassword/setNewPassword',
          })
        } else {
          // 显示支付密码输入层
          that.showInputLayer();
        }
      }
    })




  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    // 获取星钻卖出单价
    wx.request({
      url: app.globalData.baseUrl + '/currency/getCurrencyPrice',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        mark: 2 //1代表买入2代表卖出
      },
      success(res) {
        var data = res.data.data
        that.setData({
          unitPrice: data
        })
      }
    })
  },

  // 、、、、、、、
  /**
   * 显示支付密码输入层
   */
  showInputLayer: function () {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  /**
   * 隐藏支付密码输入层
   */
  hidePayLayer: function () {

    var val = this.data.pwdVal;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      pwdVal: '',
      password: val
    }, function () {
      // wx.showToast({
      //   title: val,
      // })
    });
  },
  /**
   * 获取焦点
   */
  getFocus: function () {
    this.setData({
      payFocus: true
    });
  },
  /**
   * 输入密码监听
   */
  inputPwd: function (e) {
    var that = this
    this.setData({
      pwdVal: e.detail.value
    });
    if (e.detail.value.length >= 6) {
      this.hidePayLayer();
      var that = this
      // 获取代币数
      wx.request({
        url: app.globalData.baseUrl + '/currency/getCurrencyByUser',
        data: {
          xcxOpenId: app.globalData.xcxOpenId
        },
        success(res) {
          // console.log('xingzuan')
          // console.log(that.data.number)
          if (res.data.data < that.data.number) {
            // console.log(res.data.data)
            wx.showToast({
              title: '您的代币数不足',
              icon: 'none'
            })
            //  that.setData({xjNumber:0})
          } else {
            //提交相应数据
            wx.request({
              url: app.globalData.baseUrl + '/currency/sellCurrency',
              header: {
                'content-type': 'application/x-www-form-urlencoded' // 默认值
              },
              method: 'POST',
              data: {
                xcxOpenId: app.globalData.xcxOpenId,
                num: that.data.number,
                paymentCode: that.data.password,
                total: that.data.sum.toFixed(2)
              },
              success(res) {
                if (res.code == -1) {
                  wx.showToast({
                    title: res.msg,
                  })
                } else {
                  wx.showToast({
                    title: res.msg,
                  })
                  wx.navigateTo({
                    url: '/pages/xingzuan/xingzuan',
                  })
                }

              }
            })
          }
        }
      })

    }
  }
})